const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(cc.Prefab)
    toastPref : cc.Prefab = null;
    
    @property
    _index : number = 1;

    @property(cc.Node)
    toastNode : cc.Node = null;

    onLoad(){
        this._index = 9999;
    }

    add(msg : string){
        this.addToast(msg);
    }

    addToast(msg : string){

        let toast = cc.instantiate(this.toastPref);
        this.toastNode=toast
        toast.parent = this.node;
        toast.scale  = 1;
        toast.zIndex = this._index; //插入到前方
        toast.children[0].getComponent(cc.Label).string = msg;
        toast.runAction(
            cc.sequence(
                cc.scaleTo(0.2, 1.1, 1.1),
                cc.delayTime(0.2),
                cc.scaleTo(0.3, 1, 1),
                cc.delayTime(3),
                cc.fadeTo(0.5, 0.5),
                cc.delayTime(0.2),
                cc.callFunc(()=>{
                    toast.destroy();
                },this)
            )
        );
        this._index--;
    }

    delloc(){
        if (cc.isValid(this.toastNode)) {
          cc.log("还存在")
          return true
        }else{
          cc.log("已经销毁了")
            return false
        }
    }
}
